package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.SearchResult;
import com.elinkway.infinitemovies.bean.SearchResultList;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SearchResultAdapter extends MyBaseAdapter {
	private SearchResultList searchResultList;
	
	public SearchResultList getSearchResultList() {
		return searchResultList;
	}

	private class SearchResultHolder {
		ImageView poster;
		TextView starring;
		TextView year;
		TextView name;
	}

	public SearchResultAdapter(Context context,SearchResultList searchResult) {
		super(context);
		this.searchResultList=searchResult;
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return searchResultList.getResultList().size();
	}
	public void loadMoreData(SearchResultList addResult){
		searchResultList.addResultList(addResult.getResultList());
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		SearchResultHolder holder = new SearchResultHolder();
		SearchResult result = searchResultList.getResultList().get(position);
		if(convertView == null){
			convertView = inflater.inflate(R.layout.search_result_item, null);
			holder.poster = (ImageView) convertView.findViewById(R.id.search_result_poster);
			holder.name = (TextView) convertView.findViewById(R.id.search_result_title);
			holder.year = (TextView) convertView.findViewById(R.id.search_result_year);
			holder.starring = (TextView) convertView.findViewById(R.id.search_result_actor);
			convertView.setTag(holder);
		}
		else{
			holder=(SearchResultHolder) convertView.getTag();
		}
		holder.name.setText(result.getName());
		holder.year.setText(String.format(mContext.getResources().getString(R.string.video_year)
				,TextUtils.isEmpty(result.getReleasedate())?"暂无":TextUtils.substring(result.getReleasedate(), 0, 4)));
		holder.starring.setText(String.format(mContext.getResources().getString(R.string.video_starring_actor)
				,TextUtils.isEmpty(result.getStarringname())?"暂无":result.getStarringname()));
		ImageLoader.getInstance().displayImage(result.getPoster(), holder.poster);
		return convertView;
	}
	public void resetData(SearchResultList result){
		this.searchResultList = result;
		notifyDataSetChanged();
	}
}
